package com.hitcoder.chat.emoji

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.hitcoder.chat.emoji.EmotionView.Companion.calSizeForContainEmotion
import com.rd.PageIndicatorView
import java.util.*

class EmotionPagerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    ViewPager(context, attrs) {

    private var currentWidth = -1
    private var currentHeight = -1

    fun buildEmotionViews(
        indicatorView: PageIndicatorView?,
        editText: EditText?,
        data: List<Emotion>?,
        width: Int,
        height: Int,
        delBtn: Boolean
    ) {
        if (data == null || data.isEmpty() || indicatorView == null || editText == null) {
            return
        }
        if (currentWidth == width && currentHeight == height) {
            return
        }
        currentWidth = width
        currentHeight = height
        val del = if (delBtn) 1 else 0
        var emotionViewContainSize = calSizeForContainEmotion(context, currentWidth, currentHeight)
        if (emotionViewContainSize == 0) {
            return
        }
        var pagerCount = data.size / emotionViewContainSize
        pagerCount += if (data.size % emotionViewContainSize == 0) 0 else 1
        emotionViewContainSize -= del
        var index = 0
        val emotionViews: MutableList<EmotionView> = ArrayList()
        for (i in 0 until pagerCount) {
            val emotionView = EmotionView(context, editText)
            var end = (i + 1) * emotionViewContainSize
            if (end > data.size) {
                end = data.size
            }
            emotionView.buildEmotions(data.subList(index, end), delBtn)
            emotionViews.add(emotionView)
            index = end
        }
        val mAdapter = Adapter(emotionViews)
        adapter = mAdapter
        addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                indicatorView.selection = position
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
    }

    class Adapter(private val mList: List<EmotionView>) : PagerAdapter() {
        override fun getCount(): Int {
            return mList.size
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return `object` === view
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            container.addView(mList[position])
            return mList[position]
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(mList[position])
        }
    }
}